import pandas as pd
import numpy as np
# # 5.1 发现缺失值
# # 创建 sr
# v = [ 53, None, 72, 82 ]
# k = ['1 号', '2 号', '3 号', '4 号']
# sr = pd.Series( v, index=k )
# print(sr)
# print(sr.isnull())
# # 创建 df
# v = [ [None, 1], [64, None], [72, 3], [82, 4] ]
# i = [ '1 号', '2 号', '3 号', '4 号' ]
# c = [ '年龄', '牌照' ]
# df = pd.DataFrame( v, index=i, columns=c )
# print(df)
# print(df.isnull())

# # 5.2 剔除缺失值
# # 创建 sr
# v = [ 53, None, 72, 82 ]
# k = ['1 号', '2 号', '3 号', '4 号']
# sr = pd.Series( v, index=k )
# print(sr)
# # 剔除 sr 的缺失值
# print("剔除 sr 的缺失值")
# sr = sr.dropna()
# print(sr)

# # 创建 df
# v = [ [None, None], [64, None], [72, 3], [82, 4] ]
# i = [ '1 号', '2 号', '3 号', '4 号' ]
# c = [ '年龄', '牌照' ]
# df = pd.DataFrame( v, index=i, columns=c )
# print("剔除 df 含缺失值的行")
# df = df.dropna()
# print(df)

# # 5.3 填补缺失值
# # 填充缺失值使用 .fillna() 方法，实际的数据填充没有统一的方法，很灵活。
# # 创建 sr
# v = [ 53, None, 72, 82 ]
# sr = pd.Series( v, index=['1 号', '2 号', '3 号', '4 号'] )
# print(sr)
# # # 用常数（0）填充
# # sr = sr.fillna(0)
# # print(sr)
# # # 用常数（均值）填充
# # sr = sr.fillna(np.mean(sr))
# # print(sr)
# # # 用前值填充
# # sr = sr.fillna(method= "ffill")
# # sr = sr.ffill()
# # print(sr)
# # 用后值填充
# sr = sr.fillna(method= "bfill") # 这种写法以后会废弃！！
# sr = sr.bfill() # 应该用这种
# print(sr)

# （2）二维对象
# 设定 df
v = [ [None, None], [64, None], [72, 3], [82, 4] ]
i = [ '1 号', '2 号', '3 号', '4 号' ]; c = [ '年龄', '牌照' ]
df = pd.DataFrame( v, index=i, columns=c )
print(df)

# # 用常数（0）填充
# df = df.fillna(0)
# print(df)

# # 用常数（均值）填充
# df =df.fillna(np.mean(df))
# print(df)

# # 用前值填充
# df = df.ffill()
# print(df)

# 用后值填充
df = df.bfill()
print(df)



